#include "registerwidget.h"
#include "ui_registerwidget.h"
#include "logonwidget.h"
#include "user.cpp"
#include "user.h"
//#include "mainChat.cpp"
#include<vector>
#include<QRadioButton>
extern vector<user> vec;
extern vector<user>:: iterator it;
RegisterWidget::RegisterWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::RegisterWidget)
{
    ui->setupUi(this);
    this->setWindowTitle("注册");
    this->setWindowIcon(QIcon(":/asset/image/logoicon.jpg"));
}
RegisterWidget::~RegisterWidget()
{
    delete ui;
}
void RegisterWidget::on_btn_register_clicked()
{
    //get register information
    QString regUsername = ui->reg_username_lineEdit->text();
    QString regPassword = ui->reg_password_lineEdit->text();
    QString regPasswordConfirm = ui->reg_password_conf_lineEdit->text();
    if(regPassword == regPasswordConfirm)
    {
        QString resourcePath = ":/asset/config/config.json";
        QFile jsonFile(resourcePath);
        if (jsonFile.open(QIODevice::ReadOnly | QIODevice::Text))
        {
        QByteArray jsonData = jsonFile.readAll();
        // 将 JSON 数据转换为 QVariant 对象
        QJsonParseError error;
        QJsonObject result = QJsonDocument::fromJson(jsonData, &error).object();
        QJsonObject jsonUrl = QJsonDocument::fromJson(jsonData).object();
        qDebug() << "url :" << jsonUrl;
        jsonFile.close();
        QVariantMap urlVariantMap = jsonUrl.toVariantMap();
        QMap urlMap = urlVariantMap.value("server").toMap();
        host = urlMap.value("host").toString();
        port = urlMap.value("port").toString();
        }


        //connect to server
        QNetworkRequest request(QUrl("http://"+host+':'+port+"/slimarveous/register"));//连接后端register服务
        request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
        QJsonObject json;
        json.insert("username", regUsername);
        json.insert("password", regPassword);
        QJsonDocument doc(json);
        QByteArray data = doc.toJson(QJsonDocument::Compact);
        QNetworkReply *reply = manager.post(request, data);
        connect(reply, &QNetworkReply::finished, [=]()
        {
            if (reply->error() == QNetworkReply::NoError)
            {
                // 读取响应数据
                QByteArray dataReply = reply->readAll();
                // 将 JSON 数据转换为 QVariant 对象
                QJsonParseError error;
                QJsonObject result = QJsonDocument::fromJson(dataReply, &error).object();
                // 处理 JSON 数据
                if (error.error == QJsonParseError::NoError)
                {
                    QVariantMap resultMap = result.toVariantMap();
                    //提取状态码
                    QVariant status_code = resultMap.value("status_code");
                    status_code.toString();
                    //判断注册是否有效
                    //注册有效
                    QString id=resultMap.value("id").toString();
                    if(status_code == "2")
                    {
                        QMessageBox::information(this,"success","恭喜你，注册成功！");
                        this->close();
                        LogOnWidget *w2 = new LogOnWidget(nullptr,regUsername);
                        w2->show();
                    }
                    //冲突的用户名
                    else if(status_code == "1")
                        QMessageBox::information(this,"failed","该用户名已被使用！");
                    //服务器出问题
                    else
                        QMessageBox::information(this,"failed","好像哪里出了问题呢？肯定不是服务器的问题！");


                }
            }
            else
                qDebug()<<"error";

                // 释放资源
                reply->deleteLater();

        });
    }
    else if(regPassword != regPasswordConfirm)
        QMessageBox::information(this,"failed","两次输入的密码不一致！");
}
void RegisterWidget::on_btn_return_clicked()
{
    this->close();
    LogOnWidget* w1 = new LogOnWidget(nullptr,"");
    w1->show();
}
void RegisterWidget::on_btn_cancel_clicked()
{
    ui->reg_username_lineEdit->clear();
    ui->reg_password_lineEdit->clear();
    ui->reg_password_conf_lineEdit->clear();
}
